﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Collections;

namespace LCWMS
{
    public partial class w_mst008 : DockContent
    {
        private DataSet dscustomer;
        private DataSet dscustomer_area;
        string str_sql = "";
        private int coint = 0;
        private int coint_area = 0;
        private bool b_temp = true;
        private string str_take_loc;


        public w_mst008()
        {
            InitializeComponent();
            dvg_warehouse.AutoGenerateColumns = false;
            dvg_area.AutoGenerateColumns = false;
        }
        //设置按钮可用和不可用,b_tmp=true说明可以编辑，否处于待保存状态
        private void f_setbutton(string str_tmp, bool b_tmp)
        {
            if (b_tmp)
            {
                if (dvg_warehouse.Rows.Count > 0)
                {
                    btn_new_area.Enabled = true;
                    btn_modi_area.Enabled = true;
                    btn_del_area.Enabled = true;
                }
                else
                {
                    btn_new_area.Enabled = false;
                    btn_modi_area.Enabled = false;
                    btn_del_area.Enabled = false;
                }

                btn_new.Enabled = true;
                btn_modi.Enabled = true;
                btn_del.Enabled = true;
                dvg_warehouse.Enabled = true;
                dvg_area.Enabled = true;
                btn_save.Enabled = false;
                btn_cancel.Enabled = false;
                btn_save_area.Enabled = false;
                btn_cancel_area.Enabled = false;

            }
            else
            {
                btn_new_area.Enabled = false;
                btn_modi_area.Enabled = false;
                btn_del_area.Enabled = false;
                btn_new.Enabled = false;
                btn_modi.Enabled = false;
                btn_del.Enabled = false;
                dvg_warehouse.Enabled = false;
                dvg_area.Enabled = false;
                if (str_tmp == "header") //说明是新增表头
                {
                    btn_save.Enabled = true;
                    btn_cancel.Enabled = true;
                    btn_save_area.Enabled = false;
                    btn_cancel_area.Enabled = false;
                }
                else //新增明细
                {
                    btn_save.Enabled = false;
                    btn_cancel.Enabled = false;
                    btn_save_area.Enabled = true;
                    btn_cancel_area.Enabled = true;
                }
            }
        }
        public void ClearCntrValue(Control parContainer, bool bol, string edit)
        {
            for (int index = 0; index < parContainer.Controls.Count; index++)
            {
                // 如果是容器类控件，递归调用自己
                if (parContainer.Controls[index].HasChildren)
                {
                    // ClearCntrValue(parContainer.Controls[index]);
                }
                else
                {
                    switch (parContainer.Controls[index].GetType().Name)
                    {

                        case "TextBox":

                            if (edit != "1")
                            {
                                parContainer.Controls[index].Text = "";
                            }

                            if (bol == false)
                                ((TextBox)parContainer.Controls[index]).ReadOnly = true;
                            else
                                ((TextBox)parContainer.Controls[index]).ReadOnly = false;
                            break;
                        case "RadioButton":
                            if (edit != "1")
                                ((RadioButton)(parContainer.Controls[index])).Checked = false;
                            ((RadioButton)(parContainer.Controls[index])).Enabled = bol;
                            break;
                        case "CheckBox":
                            if (edit != "1")
                                ((CheckBox)(parContainer.Controls[index])).Checked = false;

                            ((CheckBox)(parContainer.Controls[index])).Enabled = bol;
                            break;
                        case "ComboBox":
                            if (edit != "1")
                                ((ComboBox)(parContainer.Controls[index])).SelectedIndex = 0;
                            ((ComboBox)(parContainer.Controls[index])).Enabled = bol;
                            break;
                        case "DateTimePicker":
                            if (edit != "1")
                                ((DateTimePicker)(parContainer.Controls[index])).Text = "";
                            ((DateTimePicker)(parContainer.Controls[index])).Enabled = bol;
                            break;
                    }
                }
            }
            if (edit == "1")
            {
                tb_warehouse.ReadOnly = true;
                tb_area_code.ReadOnly = true;

            }
        }
        private void btn_query_Click(object sender, EventArgs e)
        {
            dscustomer = new DataSet();
            dscustomer_area = new DataSet();
            // string str_sql = "";
            string str_sql_where = " where inv_org = '" + main.g_org + "' and main_dist<>'D' ";
            //if (tb_warehouse_ch.Text.Trim() != "")
            //{
            //    str_sql_where += " and warehouse like '%" + tb_warehouse_ch.Text.Trim() + "%'";
            //}
            //判断仓库编码 (多个查询)
            if (tb_warehouse_ch.Text.Trim() != "")
            {
                string[] shopList = tb_warehouse_ch.Text.Trim().Split(',');
                string strWhere = "";
                if (shopList.Length > 0)
                {
                    foreach (string item in shopList)
                    {
                        if (strWhere == "")
                        {
                            strWhere = "'" + item + "'";
                        }
                        else
                        {
                            strWhere += ",'" + item + "'";
                        }
                    }
                    str_sql_where += " and warehouse in (" + strWhere + ")";
                }

            }

            if (tb_warehouse_name_ch.Text.Trim() != "")
            {
                str_sql_where += " and warehouse_name like '%" + tb_warehouse_name_ch.Text.Trim() + "%'";
            }
            str_sql = "select * from " + main.g_space + ".mst_warehouse  " + str_sql_where;
            try
            {
                if (main.dbtype == "SQL")
                {
                    dscustomer = main.dbservice.Sql_exec_selectsql(str_sql);
                }
                else
                {
                    dscustomer = main.dbservice.oracle_exec_selectsql(str_sql);
                }
                dvg_warehouse.DataSource = dscustomer.Tables[0];
                if (dscustomer.Tables[0].Rows.Count > 0)
                {
                    show_warehouse(dscustomer.Tables[0].Rows[0]);
                    string str_sql_area = "select * from " + main.g_space + ".mst_area where  inv_org = '" + main.g_org + "' and main_dist<>'D'and  warehouse='" + dscustomer.Tables[0].Rows[0]["warehouse"].ToString().Trim() + "' order by area_code";
                    if (main.dbtype == "SQL")
                    {
                        dscustomer_area = main.dbservice.Sql_exec_selectsql(str_sql_area);
                    }
                    else
                    {
                        dscustomer_area = main.dbservice.oracle_exec_selectsql(str_sql_area);
                    }
                    if (dscustomer_area.Tables[0].Rows.Count > 0)
                    {
                        dvg_area.DataSource = dscustomer_area.Tables[0];
                        show_area(dscustomer_area.Tables[0].Rows[0]);
                    }
                }
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to Query", main.g_info_dataset) + "!" + ee.ToString());
            }
            f_setbutton("", true);
        }

        private void show_warehouse(DataRow tr)
        {
            this.tb_warehouse.Text = tr["warehouse"].ToString().Trim();
            this.tb_warehouse_name.Text = tr["warehouse_name"].ToString().Trim();
            this.cbx_warehouse_flag.SelectedValue = tr["warehouse_flag"].ToString().Trim();
            this.tb_memo_1.Text = tr["memo"].ToString().Trim();
            if (tr["putaway_flag"].ToString().Trim().ToUpper() == "1")
            {
                cbx_putaway_flag.Text = "YES";
            }
            else
            {
                cbx_putaway_flag.Text = "NO";
            }
            if (tr["alloc_flag"].ToString().Trim().ToUpper() == "1")
            {
                cbx_alloc_flag.Text = "YES";
            }
            else
            {
                cbx_alloc_flag.Text = "NO";
            }
        }

        private void show_area(DataRow tr)
        {
            this.tb_area_code.Text = tr["area_code"].ToString().Trim();
            tb_belong_area.Text = tr["belong_area"].ToString().Trim();
            this.tb_area_name.Text = tr["area_name"].ToString().Trim();
            switch (tr["area_flag"].ToString().Trim())
            {
                case "0":
                    this.cbx_area_flag.Text = main.g_class.comm_getchangestring("Stock Area", main.g_info_dataset);
                    break;
                case "1":
                    this.cbx_area_flag.Text = main.g_class.comm_getchangestring("PA Area", main.g_info_dataset);
                    break;
                case "2":
                    this.cbx_area_flag.Text = main.g_class.comm_getchangestring("Picking area", main.g_info_dataset);
                    break;
            }
            if (tr["putaway_flag"].ToString().Trim().ToUpper() == "1")
            {
                cbx_putaway_area.Text = "YES";
            }
            else
            {
                cbx_putaway_area.Text = "NO";
            }
            if (tr["alloc_flag"].ToString().Trim().ToUpper() == "1")
            {
                cbx_alloc_area.Text = "YES";
            }
            else
            {
                cbx_alloc_area.Text = "NO";
            }
            this.tb_standby_1.Text = tr["standby_1"].ToString().Trim();
            this.tb_standby_2.Text = tr["standby_2"].ToString().Trim();
            this.tb_standby_3.Text = tr["standby_3"].ToString().Trim();
            this.tb_standby_4.Text = tr["standby_4"].ToString().Trim();
            this.tb_area_code_all.Text = tr["area_code"].ToString().Trim();
            tb_belong_area.Text = tr["belong_area"].ToString().Trim();
            this.tb_memo.Text = tr["memo"].ToString().Trim();

        }

        private void w_mst008_Load(object sender, EventArgs e)
        {
            DataSet ds_type = new DataSet();
            DataSet ds_tax;
            string str_common = "";

            //获取按钮权限
            str_take_loc = "1";
            string str_right;

            str_right = main.g_class.comm_getright("w_mst008", main.g_right_dataset);

            //新增

            if (str_right.Substring(0, 1) == "0")
            {
                btn_new.Visible = false;
                btn_new_area.Visible = false;
            }

            else
            {

                btn_new.Visible = true;

                btn_new_area.Visible = true;

            }

            //修改

            if (str_right.Substring(1, 1) == "0")
            {

                btn_modi.Visible = false;

                btn_modi_area.Visible = false;

            }

            else
            {

                btn_modi.Visible = true;

                btn_modi_area.Visible = true;

            }

            //删除

            if (str_right.Substring(2, 1) == "0")
            {

                btn_del.Visible = false;

                btn_del_area.Visible = false;

            }

            else
            {

                btn_del.Visible = true;

                btn_del_area.Visible = true;

            }

            //保存，取消

            if (str_right.Substring(3, 1) == "0")
            {

                btn_save.Visible = false;

                btn_cancel.Visible = false;

                btn_save_area.Visible = false;

                btn_cancel_area.Visible = false;

            }

            else
            {

                btn_save.Visible = true;

                btn_cancel.Visible = true;

                btn_save_area.Visible = true;

                btn_cancel_area.Visible = true;

            }

            str_sql = "select * from " + main.g_space + ".mst_warehouse  where 1=2" ;
            try
            {
                if (main.dbtype == "SQL")
                {
                    dscustomer = main.dbservice.Sql_exec_selectsql(str_sql);
                }
                else
                {
                    dscustomer = main.dbservice.oracle_exec_selectsql(str_sql);
                }
                dvg_warehouse.DataSource = dscustomer.Tables[0];
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(ee.Message);
            }

            //得到库位编码的生成方式str_take_loc
            try
            {
                string str_sql2 = "select id_data from " + main.g_space + ".sys_parameter where inv_org='" + main.g_org + "' and id = 8019";
                if (main.dbtype == "SQL")
                {
                    str_take_loc = main.dbservice.Sql_exec_selectstring(str_sql2).Trim();
                }
                else
                {
                    str_take_loc = main.dbservice.oracle_exec_selectstring(str_sql2).Trim();
                }
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(ee.ToString());
                return;
            }

            try
            {
                //if (main.dbtype == "SQL")
                //{
                //    str_common = "select dictionary_code,case use_flag when '1' then content1 when '2' then content2 when '3' then convert(char,number1) else convert(char,number2) end as warehouse_flag  from " + main.g_space + ".sys_dictionary where inv_org = '" + main.g_org + "' and ltrim(rtrim(dictionary_name))='warehouse_flag ' order by dictionary_code";
                //    ds_type = main.dbservice.Sql_exec_selectsql(str_common);
                //}
                //else
                //{
                //    str_common = "select dictionary_code,case use_flag when '1' then content1 when '2' then content2 when '3' then to_char(number1) else to_char(number2) end as warehouse_flag  from " + main.g_space + ".sys_dictionary where inv_org = '" + main.g_org + "' and trim(dictionary_name)='warehouse_flag' order by dictionary_code";
                //    ds_type = main.dbservice.oracle_exec_selectsql(str_common);
                //}
                ////进行语言转换
                //int i_ds;
                //i_ds = ds_type.Tables[0].Rows.Count;
                //if (i_ds > 0)
                //{
                //    for (int i = 0; i < i_ds; i++)
                //    {
                //        ds_type.Tables[0].Rows[i]["warehouse_flag"] = main.g_class.comm_getchangestring(ds_type.Tables[0].Rows[i]["warehouse_flag"].ToString().Trim(), main.g_info_dataset);
                //        ds_type.Tables[0].Rows[i]["dictionary_code"] = main.g_class.comm_getchangestring(ds_type.Tables[0].Rows[i]["dictionary_code"].ToString().Trim(), main.g_info_dataset);
                //    }
                //}
                //DataRow dr = ds_type.Tables[0].NewRow();
                //dr["dictionary_code"] = "";
                //dr["warehouse_flag"] = "";
                //ds_type.Tables[0].Rows.Add(dr);
                //cbx_warehouse_flag.DataSource = ds_type.Tables[0];
                //cbx_warehouse_flag.ValueMember = "dictionary_code";
                //cbx_warehouse_flag.DisplayMember = "warehouse_flag";
                //cbx_warehouse_flag.Text = "";

                //初始化保税区分
                if (main.dbtype == "SQL")
                {
                    str_common = "select dictionary_code,case use_flag when '1' then content1 when '2' then content2 when '3' then convert(char,number1) else convert(char,number2) end as tax_dist  from " + main.g_space + ".sys_dictionary where inv_org = '" + main.g_org + "' and ltrim(rtrim(dictionary_name))='tax_dist' order by dictionary_code";
                    ds_tax = main.dbservice.Sql_exec_selectsql(str_common);
                }
                else
                {
                    str_common = "select dictionary_code,case use_flag when '1' then content1 when '2' then content2 when '3' then to_char(number1) else to_char(number2) end as tax_dist  from " + main.g_space + ".sys_dictionary where inv_org = '" + main.g_org + "' and trim(dictionary_name)='tax_dist' order by dictionary_code";
                    ds_tax = main.dbservice.oracle_exec_selectsql(str_common);
                }

                cbx_warehouse_flag.ValueMember = "dictionary_code";
                cbx_warehouse_flag.DisplayMember = "tax_dist";
                cbx_warehouse_flag.DataSource = ds_tax.Tables[0].DefaultView;
                cbx_warehouse_flag.SelectedValue = "0";
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to initialize Information of warehouse_flag", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }
            int i_cbx;
            ArrayList al_cbx = new ArrayList();
            main.g_class.comm_changeControl(this, main.g_win_dataset);
            main.g_class.comm_changeCbxContent(this.cbx_area_flag, main.g_info_dataset, '1');
            //几个下拉框的转换
            //cbx_asnstatus
            i_cbx = cbx_area_flag.Items.Count;
            if (i_cbx > 0)
            {
                for (int i = 0; i < i_cbx; i++)
                {
                    if (cbx_area_flag.Items[i].ToString().Trim() != "")
                        al_cbx.Add(main.g_class.comm_getchangestring(cbx_area_flag.Items[i].ToString().Trim(), main.g_info_dataset));
                }
                //清除cbx_asnstatus
                cbx_area_flag.Items.Clear();
                //在加入
                i_cbx = al_cbx.Count;
                for (int i = 0; i < i_cbx; i++)
                {
                    cbx_area_flag.Items.Add(al_cbx[i]);
                }
                //出来在加上空值
                //cbx_area_flag.Items.Add("");
            }
            ClearCntrValue(this.groupBox2, false, "0");
            ClearCntrValue(this.groupBox4, false, "0");
            //初始化按钮
            f_setbutton("", true);
        }

        private void dvg_warehouse_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dscustomer_area = new DataSet();
            DataRow[] tr = dscustomer.Tables[0].Select("warehouse='" + dvg_warehouse.Rows[dvg_warehouse.CurrentRow.Index].Cells["warehouse"].Value.ToString() + "'");
            ClearCntrValue(this.groupBox2, false, "0");
            show_warehouse(tr[0]);
            string str_sql_area = "select * from " + main.g_space + ".mst_area where inv_org = '" + main.g_org + "' and main_dist<>'D'and  warehouse='" + this.tb_warehouse.Text.Trim() + "' order by area_code";
            if (main.dbtype == "SQL")
            {
                dscustomer_area = main.dbservice.Sql_exec_selectsql(str_sql_area);
            }
            else
            {
                dscustomer_area = main.dbservice.oracle_exec_selectsql(str_sql_area);
            }
            dvg_area.DataSource = dscustomer_area.Tables[0];
            ClearCntrValue(this.groupBox4, false, "0");
            if (dscustomer_area.Tables[0].Rows.Count > 0)
            {
                show_area(dscustomer_area.Tables[0].Rows[0]);
            }

        }

        private void dvg_area_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dscustomer_area.Tables[0].Rows.Count > 0)
            {
                DataRow[] tr = dscustomer_area.Tables[0].Select("area_code='" + dvg_area.Rows[dvg_area.CurrentRow.Index].Cells[0].Value.ToString() + "'");
                if (tr.Length > 0)
                    show_area(tr[0]);
            }
        }

        private void btn_new_Click(object sender, EventArgs e)
        {
            coint = 1;
            //控制按钮不可用
            f_setbutton("header", false);
            ClearCntrValue(this.groupBox2, true, "0");
            cbx_putaway_flag.Text = "Yes";
            cbx_alloc_flag.Text = "Yes";
           // ClearCntrValue(this.groupBox4, true, "0");
            dvg_area.DataSource = null;
            tb_area_code.Text = "";
            tb_belong_area.Text = "";
            tb_area_name.Text = "";
            tb_standby_1.Text = "";
            tb_standby_2.Text = "";

        }

        private void btn_modi_Click(object sender, EventArgs e)
        {
            if (tb_warehouse.Text.Trim() == "")
            {
                MessageBox.Show("必须选择一个仓库进行修改!");
                return;
            }
            coint = 2;
            f_setbutton("header", false);
            ClearCntrValue(this.groupBox2, true, "1");
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            DataSet ds_tds = new DataSet();
            int dount = 0;
            if (tb_warehouse.Text.Trim() == "")
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Please Choose a bill which need to delete", main.g_info_dataset) + "!");
                return;
            }
            if (MessageBox.Show("确认删除 " + tb_warehouse.Text.Trim()+ " ?", main.g_class.comm_getchangestring("Information", main.g_info_dataset), MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                return;
            }
            string str_temp = "";
            if (main.dbtype == "SQL")
            {
                str_temp = str_temp + "update_time=getdate(),";
            }
            else
            {
                str_temp = str_temp + "update_time= sysdate,";
            }

            string str_sql = "update " + main.g_space + ".mst_warehouse set main_dist='D'," + str_temp + "updater='" + main.g_user + "'  where inv_org = '" + main.g_org + "' and warehouse='" + this.tb_warehouse.Text.Trim() + "'";
            //if (MessageBox.Show(main.g_class.comm_getchangestring("Are you sure to delete ,Information ", main.g_info_dataset) + this.tb_warehouse.Text.Trim() + "？" +  MessageBoxButtons.OKCancel) == DialogResult.OK)

            string sql_area = "select count(*) from " + main.g_space + ".mst_area where inv_org = '" + main.g_org + "' and warehouse='" + tb_warehouse.Text.Trim() + "' and main_dist <> 'D'";

            try
            {
                if (main.dbtype == "SQL")
                    dount = main.dbservice.Sql_exec_selectint(sql_area);
                else
                    dount = main.dbservice.oracle_exec_selectint(sql_area);

            }
            catch (System.Exception ee)
            {
                MessageBox.Show(this.tb_warehouse.Text.Trim() + main.g_class.comm_getchangestring("Failed to delete the consignor", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }
            if (dount <= 0)
            {
                try
                {
                    if (main.dbtype == "SQL")
                    {
                        main.dbservice.Sql_exec_commandsql(str_sql);
                    }
                    else
                    {
                        main.dbservice.oracle_exec_commandsql(str_sql);
                    }
                    b_temp = false;
                    btn_query_Click(sender, new EventArgs());
                    b_temp = true;

                }
                catch (System.Exception ee)
                {
                    MessageBox.Show(this.tb_warehouse.Text.Trim() + main.g_class.comm_getchangestring("Failed to delete the owner", main.g_info_dataset) + "!" + ee.ToString());
                    return;
                }
            }
            else
            {
                MessageBox.Show(this.tb_warehouse.Text.Trim() + main.g_class.comm_getchangestring("Failed to delete the owner", main.g_info_dataset) + "!");
                return;
            }

        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            string str_temp = "", s_alloc_flag = "", s_putaway_flag = "", s_temp = "", s_warehouse_flag = "";
            //判断仓库名称是否输入
            if (tb_warehouse.Text.Trim() == "")
            {               
                MessageBox.Show(main.g_class.comm_getchangestring("请输入仓库编码!", main.g_info_dataset) + "!");
                tb_warehouse.Focus();
                return;
               
            }
            if (tb_warehouse_name.Text.Trim() == "")
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Please input a warehouse name", main.g_info_dataset) + "!");
                tb_warehouse_name.Focus();
                return;
            }
            //判断是否报税仓库
            s_temp = main.g_class.comm_getconversestring(this.cbx_warehouse_flag.Text.ToString().Trim(), main.g_info_dataset).ToUpper();
            if (cbx_warehouse_flag.Text.ToString().Trim() == "保税")
            {
                s_warehouse_flag = "1";
            }
            else
            {
                s_warehouse_flag = "0";
            }
            //判断是否上架
            s_temp = main.g_class.comm_getconversestring(this.cbx_putaway_flag.Text.ToString().Trim(), main.g_info_dataset).ToUpper();
            if (s_temp.Trim() == "YES")
            {
                s_putaway_flag = "1";
            }
            else
            {
                s_putaway_flag = "0";
            }
            //判断是否分配
            s_temp = main.g_class.comm_getconversestring(this.cbx_alloc_flag.Text.ToString().Trim(), main.g_info_dataset).ToUpper();
            if (s_temp.Trim() == "YES")
            {
                s_alloc_flag = "1";
            }
            else
            {
                s_alloc_flag = "0";
            }

            if (coint == 1)//添加
            {
                string str_pk = "select count(*) from " + main.g_space + ".mst_warehouse where inv_org = '" + main.g_org + "' and warehouse='" + this.tb_warehouse.Text.Trim() + "'";
                str_sql = "insert into  " + main.g_space + ".mst_warehouse (" +
                                "inv_org,warehouse,warehouse_name,warehouse_flag,memo,main_dist,putaway_flag,alloc_flag,made_time,maker) values(" +
                  "'" + main.g_org + "','" + (this.tb_warehouse.Text.Trim().ToUpper()) + "','" + this.tb_warehouse_name.Text.Trim() + "','" + s_warehouse_flag + "','" + this.tb_memo_1.Text.Trim() + "','A','" + s_putaway_flag + "','" + s_alloc_flag + "',";
                int dount = -1;
                if (main.dbtype == "SQL")
                {
                    dount = main.dbservice.Sql_exec_selectint(str_pk);
                    str_temp = str_temp + "getdate(),'";
                }
                else
                {
                    dount = main.dbservice.oracle_exec_selectint(str_pk);
                    str_temp = str_temp + " sysdate,'";
                }
                if (dount > 0)
                {
                    MessageBox.Show(main.g_class.comm_getchangestring("The code of warehouse has already exists", main.g_info_dataset) + "!");
                    this.tb_warehouse.Focus();
                    return;
                }
                str_temp = str_temp + main.g_user + "')";
                try
                {
                    str_sql += str_temp;
                    if (main.dbtype == "SQL")
                    {
                        main.dbservice.Sql_exec_commandsql(str_sql);
                    }
                    else
                    {
                        main.dbservice.oracle_exec_commandsql(str_sql);
                    }
                    // btn_query_Click(sender, new EventArgs());
                }
                catch (System.Exception ee)
                {
                    MessageBox.Show(main.g_class.comm_getchangestring("Fail to save", main.g_info_dataset) + "!" + ee.ToString());
                    Cursor.Current = Cursors.Default;
                    return;
                }
                //if (dscustomer != null)
                //{
                //    DataRow tr = dscustomer.Tables[0].NewRow();
                //    tr["warehouse"] = this.tb_warehouse.Text.Trim();
                //    tr["warehouse_name"] = this.tb_warehouse_name.Text.Trim();
                //    tr["warehouse_flag"] = this.cbx_warehouse_flag.SelectedValue.ToString().Trim();
                //    tr["memo"] = this.tb_memo_1.Text.Trim();

                //    dscustomer.Tables[0].Rows.Add(tr);
                //    dvg_warehouse.DataSource = dscustomer.Tables[0];

                //}
            }
            else if (coint == 2)
            {
                ArrayList al_sql = new ArrayList();
                DataSet ds_flag = new DataSet();
                if (main.dbtype == "SQL")
                {
                    str_temp = str_temp + "update_time=getdate(),";
                }
                else
                {
                    str_temp = str_temp + "update_time= sysdate,";
                }
                str_temp = str_temp + "updater='" + main.g_user + "' where inv_org = '" + main.g_org + "' and warehouse='" + this.tb_warehouse.Text.Trim() + "'";
                str_sql = "update " + main.g_space + ".mst_warehouse set warehouse_name='" + this.tb_warehouse_name.Text.Trim() + "',warehouse_flag='" + s_warehouse_flag.Trim() + "',putaway_flag='" + s_putaway_flag.Trim() + "',alloc_flag='" + s_alloc_flag.Trim() + "',memo='" + this.tb_memo_1.Text.Trim() + "',main_dist='U'," + str_temp;
                al_sql.Add(str_sql);
                //////判断是否修改了上架计算标志和拣货计算标志
                str_temp = "select putaway_flag,alloc_flag from " + main.g_space + ".mst_warehouse where inv_org = '" + main.g_org + "' and warehouse='" + this.tb_warehouse.Text.Trim() + "'";
                try
                {
                    if (main.dbtype == "SQL")
                    {
                        ds_flag = main.dbservice.Sql_exec_selectsql(str_temp);
                    }
                    else
                    {
                        ds_flag = main.dbservice.oracle_exec_selectsql(str_temp);
                    }
                    if (s_alloc_flag.Trim() != ds_flag.Tables[0].Rows[0]["alloc_flag"].ToString().Trim() || s_putaway_flag.Trim() != ds_flag.Tables[0].Rows[0]["putaway_flag"].ToString().Trim())
                    {
                        str_sql = "update " + main.g_space + ".mst_location set main_dist = 'C'";
                        if (s_alloc_flag.Trim() != ds_flag.Tables[0].Rows[0]["alloc_flag"].ToString().Trim()) //说明分配计算改变了
                        {
                            str_sql = str_sql + ",alloc_flag = '" + s_alloc_flag.Trim() + "'";
                        }
                        if (s_putaway_flag.Trim() != ds_flag.Tables[0].Rows[0]["putaway_flag"].ToString().Trim()) //说明上架计算改变了
                        {
                            str_sql = str_sql + ",putaway_flag = '" + s_putaway_flag.Trim() + "'";
                        }
                        str_sql = str_sql + " where exists (select 1 from " + main.g_space + ".mst_area b," + main.g_space + ".mst_warehouse c where b.inv_org=c.inv_org and b.warehouse = c.warehouse" +
                                  " and b.inv_org = " + main.g_space + ".mst_location.inv_org and b.area_code = " + main.g_space + ".mst_location.area_code and c.warehouse = '" + this.tb_warehouse.Text.Trim() + "' and c.inv_org = '" + main.g_org + "')";
                        al_sql.Add(str_sql);
                    }
                    string[] str_command_sql = new string[al_sql.Count];
                    for (int i = 0; i < al_sql.Count; i++)
                    {
                        str_command_sql[i] = al_sql[i].ToString().Trim();
                    }
                    if (main.dbtype == "SQL")
                    {
                        main.dbservice.Sql_exec_mutilsql(str_command_sql);
                    }
                    else
                    {
                        main.dbservice.oracle_exec_mutilsql(str_command_sql);
                    }
                   
                }
                catch (System.Exception ee)
                {
                    MessageBox.Show(main.g_class.comm_getchangestring("Fail to save", main.g_info_dataset) + "!" + ee.ToString());
                    Cursor.Current = Cursors.Default;
                    return;
                }
            }
            //btn_query_Click(sender, new EventArgs());
            if (coint == 1)
            {
                b_temp = false;
                DataRow newdr = ((DataTable)dvg_warehouse.DataSource).NewRow();
                newdr["warehouse"] = tb_warehouse.Text.Trim();
                newdr["warehouse_name"] = tb_warehouse.Text.Trim();
                ((DataTable)dvg_warehouse.DataSource).Rows.Add(newdr);
                //选中这一行
                for (int i = 0; i < dvg_warehouse.RowCount; i++)
                {
                    if (i == dvg_warehouse.RowCount - 1)
                    {
                        dvg_warehouse.Rows[i].Selected = true;
                        dvg_warehouse.CurrentCell = dvg_warehouse.Rows[i].Cells["warehouse"];
                    }
                    else
                        dvg_warehouse.Rows[i].Selected = false;
                }
                try
                {
                    string str_sql_area = "select * from " + main.g_space + ".mst_area where inv_org = '" + main.g_org + "' and main_dist<>'D'and  warehouse='" + this.tb_warehouse.Text.Trim() + "' order by area_code";
                    if (main.dbtype == "SQL")
                    {
                        dscustomer_area = main.dbservice.Sql_exec_selectsql(str_sql_area);
                    }
                    else
                    {
                        dscustomer_area = main.dbservice.oracle_exec_selectsql(str_sql_area);
                    }
                    dvg_area.DataSource = dscustomer_area.Tables[0];
                }
                catch (System.Exception ee)
                {
                    MessageBox.Show(ee.Message);
                }
                b_temp = true;
            }
            f_setbutton("", true);
            ClearCntrValue(this.groupBox2, false, "1");
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            f_setbutton("header", true);
            ClearCntrValue(this.groupBox2, false, "0");
         
            dvg_warehouse_SelectionChanged(sender, new EventArgs());

        }

        private void btn_new_area_Click(object sender, EventArgs e)
        {
            if (tb_warehouse.Text.Trim() == "") //说明没有选择订单
            {
                MessageBox.Show(main.g_class.comm_getchangestring(" Please Choose a warehouse which need to Add", main.g_info_dataset) + "!");
                return;
            }
            coint_area = 1;
            //控制按钮不可用
            f_setbutton("detail", false);
            ClearCntrValue(this.groupBox4, true, "0");
            cbx_putaway_area.Text = cbx_putaway_flag.Text.Trim();
            cbx_alloc_area.Text = cbx_alloc_flag.Text.Trim();
        }

        private void btn_modi_area_Click(object sender, EventArgs e)
        {
            if (tb_area_code.Text.Trim() == "")
            {
                MessageBox.Show("必须选择要修改的区域!");
                return;
            }
            coint_area = 2;
            f_setbutton("detail", false);
            ClearCntrValue(this.groupBox4, true, "1");
        }

        private void btn_del_area_Click(object sender, EventArgs e)
        {

            DataSet ds_tds = new DataSet();
            int dount = 0;
            if (tb_area_code.Text.Trim() == "")
            {
                MessageBox.Show("必须选择要删除的区域!");
                return;
            }
            if (MessageBox.Show("确认删除 " + tb_area_code.Text.Trim() + " ?", main.g_class.comm_getchangestring("Information", main.g_info_dataset), MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            {
                return;
            }
            string str_temp = "";
            if (main.dbtype == "SQL")
            {
                str_temp = str_temp + "update_time=getdate(),";
            }
            else
            {
                str_temp = str_temp + "update_time= sysdate,";
            }

            string str_sql = "update " + main.g_space + ".mst_area set main_dist='D'," + str_temp + "updater='" + main.g_user + "'  where inv_org = '" + main.g_org + "' and area_code='" + this.tb_area_code_all.Text.Trim() + "'";
            // if (MessageBox.Show(main.g_class.comm_getchangestring("Are you sure to delete ,Information", main.g_info_dataset) + this.tb_area_code_all.Text.Trim() + "？" + MessageBoxButtons.OKCancel) == DialogResult.OK)               

            string sql_area = "select count(*) from " + main.g_space + ".mst_location where inv_org = '" + main.g_org + "' and area_code='" + tb_area_code_all.Text.Trim() + "' and  main_dist <> 'D'";

            try
            {
                if (main.dbtype == "SQL")
                    dount = main.dbservice.Sql_exec_selectint(sql_area);
                else
                    dount = main.dbservice.oracle_exec_selectint(sql_area);
                // btn_query_Click(sender, new EventArgs());

            }
            catch (System.Exception ee)
            {
                MessageBox.Show(this.tb_area_code_all.Text.Trim() + main.g_class.comm_getchangestring("Failed to delete", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }
            if (dount <= 0)
            {
                try
                {
                    if (main.dbtype == "SQL")
                        main.dbservice.Sql_exec_commandsql(str_sql);
                    else
                        main.dbservice.oracle_exec_commandsql(str_sql);
                    btn_query_Click(sender, new EventArgs());

                }
                catch (System.Exception ee)
                {
                    MessageBox.Show(this.tb_area_code_all.Text.Trim() + main.g_class.comm_getchangestring("Failed to delete", main.g_info_dataset) + "!" + ee.ToString());
                    return;
                }
            }
            else
            {
                MessageBox.Show(this.tb_area_code_all.Text.Trim() +" 区域下存在库位，请先删除库位!");
                return;
            }


        }

        private void btn_save_area_Click(object sender, EventArgs e)
        {
            string str_temp = "", str_belong_area = "", s_putaway_flag = "", s_alloc_flag="";
            decimal d_standby1, d_standby2;
            string str_get_newarea = "";

            if (tb_standby_1.Text.Trim() == "")
                d_standby1 = 0;
            else 
                d_standby1 = System.Convert.ToDecimal(tb_standby_1.Text.Trim());
            if (tb_standby_2.Text.Trim() == "")
                d_standby2 = 0;
            else
                d_standby2 = System.Convert.ToDecimal(tb_standby_2.Text.Trim());
            if (tb_area_code.Text.Trim() == "")
            {
                MessageBox.Show("必须输入区域编码！");
                tb_area_code.Focus();
                return;
            }
            //判断区域名是否输入
            if (tb_area_name.Text.Trim() == "")
            {
                MessageBox.Show(main.g_class.comm_getchangestring("必须输入区域名称", main.g_info_dataset) + "!");
                tb_area_name.Focus();
                return;
            }
            //判断是否上架
            str_temp = main.g_class.comm_getconversestring(this.cbx_putaway_area.Text.ToString().Trim(), main.g_info_dataset).ToUpper();
            if (str_temp.Trim() == "YES")
            {
                s_putaway_flag = "1";
            }
            else
            {
                s_putaway_flag = "0";
            }
            //判断是否分配
            str_temp = main.g_class.comm_getconversestring(this.cbx_alloc_area.Text.ToString().Trim(), main.g_info_dataset).ToUpper();
            if (str_temp.Trim() == "YES")
            {
                s_alloc_flag = "1";
            }
            else
            {
                s_alloc_flag = "0";
            }
            str_temp = "";
            if (coint_area == 1)//添加
            {
                if (tb_belong_area.Text.Trim() != "")
                {
                    str_belong_area = tb_belong_area.Text.Trim();
                }
                else
                {
                    if (str_take_loc.Trim() == "0") //不需要前缀
                    {
                        str_belong_area = this.tb_area_code.Text.Trim().ToUpper();
                    }
                    else
                    {
                        str_belong_area = this.tb_warehouse.Text.Trim().ToUpper() + this.tb_area_code.Text.Trim().ToUpper();
                    }
                    tb_belong_area.Text = str_belong_area;
                }
                if (str_take_loc.Trim() == "0") //不需要前缀
                {
                    str_get_newarea = this.tb_area_code.Text.Trim().ToUpper();
                }
                else  //需要前缀
                {
                    str_get_newarea = this.tb_warehouse.Text.Trim().ToUpper() + this.tb_area_code.Text.Trim().ToUpper();
                }

                string str_pk = "select count(*) from " + main.g_space + ".mst_area where inv_org = '" + main.g_org + "' and area_code='" + str_get_newarea.Trim() + "'";
                str_sql = "insert into  " + main.g_space + ".mst_area (inv_org,area_code,belong_area,warehouse,area_name,area_flag, " +
                                "standby_1,standby_2,standby_3,standby_4,memo,main_dist,made_time,maker,putaway_flag,alloc_flag) values(" +
                  "'" + main.g_org + "','" + str_get_newarea.Trim() + "','" + str_belong_area.Trim() + "','" + this.tb_warehouse.Text.Trim() + "'," +
                  "'" + this.tb_area_name.Text.Trim() + "','" + this.cbx_area_flag.SelectedIndex.ToString().Trim() + "'," +
                  " " + d_standby1 + "," + d_standby2 + "," +
                  "'" + tb_standby_3.Text.Trim() + "','" + tb_standby_4.Text.Trim() + "','" + tb_memo.Text.Trim() + "','A',";
                int dount = -1;
                if (main.dbtype == "SQL")
                {
                    dount = main.dbservice.Sql_exec_selectint(str_pk);
                    str_temp = str_temp + "getdate(),'";
                }
                else
                {
                    dount = main.dbservice.oracle_exec_selectint(str_pk);
                    str_temp = str_temp + " sysdate,'";
                }
                if (dount > 0)
                {
                    MessageBox.Show(main.g_class.comm_getchangestring("The area code has already exists", main.g_info_dataset) + "!");
                    this.tb_warehouse.Focus();
                    return;
                }
                str_temp = str_temp + main.g_user + "','"+s_putaway_flag+"','"+s_alloc_flag+"')";
                try
                {
                    str_sql += str_temp;
                    if (main.dbtype == "SQL")
                    {
                        main.dbservice.Sql_exec_commandsql(str_sql);
                    }
                    else
                    {
                        main.dbservice.oracle_exec_commandsql(str_sql);
                    }
                    // btn_query_Click(sender, new EventArgs());
                }
                catch (System.Exception ee)
                {
                    MessageBox.Show(main.g_class.comm_getchangestring("Fail to save", main.g_info_dataset) + "!" + ee.ToString());
                    Cursor.Current = Cursors.Default;
                    return;
                }
                if (dscustomer != null)
                {
                    DataRow tr = dscustomer_area.Tables[0].NewRow();
                    tr["area_code"] = str_get_newarea.Trim();
                    tr["warehouse"] = this.tb_warehouse.Text.Trim();
                    tr["area_name"] = this.tb_area_name.Text.Trim();
                    tr["area_flag"] = this.cbx_area_flag.SelectedIndex.ToString().Trim();
                    tr["belong_area"] = this.tb_belong_area.Text.Trim();
                    tr["memo"] = this.tb_memo.Text.Trim();

                    dscustomer_area.Tables[0].Rows.Add(tr);
                    dvg_area.DataSource = dscustomer_area.Tables[0];
                    dvg_area.Rows[dvg_area.CurrentRow.Index].Selected = false;
                    dvg_area.Rows[dvg_area.Rows.Count - 1].Selected = true;
                    this.dvg_area.CurrentCell = this.dvg_area.Rows[dvg_area.Rows.Count - 1].Cells[0];
                    if (dscustomer_area.Tables[0].Rows.Count > 0)
                    {
                        DataRow[] tr2 = dscustomer_area.Tables[0].Select("area_code='" + dvg_area.Rows[dvg_area.Rows.Count - 1].Cells[0].Value.ToString() + "'");
                        if (tr2.Length > 0)
                            show_area(tr2[0]);
                    }

                }
            }
            else if (coint_area == 2)
            {
                ArrayList al_sql = new ArrayList();
                DataSet ds_flag;
                if (tb_belong_area.Text.Trim() != "")
                {
                    str_belong_area = tb_belong_area.Text.Trim();
                }
                else
                {
                    str_belong_area = tb_area_code.Text.Trim();
                    tb_belong_area.Text = str_belong_area;
                }
                if (main.dbtype == "SQL")
                {
                    str_temp = str_temp + "update_time=getdate(),";
                }
                else
                {
                    str_temp = str_temp + "update_time= sysdate,";
                }
                str_temp = str_temp + "updater='" + main.g_user + "' where inv_org = '" + main.g_org + "' and area_code='" + this.tb_area_code.Text.Trim() + "'";
                str_sql = "update " + main.g_space + ".mst_area set belong_area='" + str_belong_area.Trim() + "', area_name='" + this.tb_area_name.Text.Trim() + "'," +
                    "area_flag='" + this.cbx_area_flag.SelectedIndex.ToString().Trim() + "',standby_1 =" + d_standby1 + "," +
                    "standby_2=" + d_standby2 + ",standby_3 ='" + this.tb_standby_3.Text.Trim() + "'," +
                    "standby_4 ='" + this.tb_standby_4.Text.Trim() + "',memo='" + this.tb_memo.Text.Trim() + "',main_dist='U',putaway_flag='" + s_putaway_flag + "',alloc_flag='"+s_alloc_flag+"'," + str_temp;
                al_sql.Add(str_sql);
                //////判断是否修改了上架计算标志和拣货计算标志
                str_temp = "select putaway_flag,alloc_flag from " + main.g_space + ".mst_area where inv_org = '" + main.g_org + "' and area_code='" + this.tb_area_code.Text.Trim() + "'";
                try
                {
                    if (main.dbtype == "SQL")
                    {
                        ds_flag = main.dbservice.Sql_exec_selectsql(str_temp);
                    }
                    else
                    {
                        ds_flag = main.dbservice.oracle_exec_selectsql(str_temp);
                    }
                    if (s_alloc_flag.Trim() != ds_flag.Tables[0].Rows[0]["alloc_flag"].ToString().Trim() || s_putaway_flag.Trim() != ds_flag.Tables[0].Rows[0]["putaway_flag"].ToString().Trim())
                    {
                        str_sql = "update " + main.g_space + ".mst_location set main_dist = 'C'";
                        if (s_alloc_flag.Trim() != ds_flag.Tables[0].Rows[0]["alloc_flag"].ToString().Trim()) //说明分配计算改变了
                        {
                            str_sql = str_sql + ",alloc_flag = '" + s_alloc_flag.Trim() + "'";
                        }
                        if (s_putaway_flag.Trim() != ds_flag.Tables[0].Rows[0]["putaway_flag"].ToString().Trim()) //说明上架计算改变了
                        {
                            str_sql = str_sql + ",putaway_flag = '" + s_putaway_flag.Trim() + "'";
                        }
                        str_sql = str_sql + " where exists (select 1 from " + main.g_space + ".mst_area b," + main.g_space + ".mst_warehouse c where b.inv_org=c.inv_org and b.warehouse = c.warehouse" +
                                  " and b.inv_org = " + main.g_space + ".mst_location.inv_org and b.area_code = " + main.g_space + ".mst_location.area_code and c.warehouse = '" + this.tb_warehouse.Text.Trim() + "' and c.inv_org = '" + main.g_org + "')";
                        al_sql.Add(str_sql);


                    }

                    string[] str_command_sql = new string[al_sql.Count];
                    for (int i = 0; i < al_sql.Count; i++)
                    {
                        str_command_sql[i] = al_sql[i].ToString().Trim();
                    }
                    if (main.dbtype == "SQL")
                    {
                        main.dbservice.Sql_exec_mutilsql(str_command_sql);
                    }
                    else
                    {
                        main.dbservice.oracle_exec_mutilsql(str_command_sql);
                    }
                    string str_sql_area = "select * from " + main.g_space + ".mst_area where inv_org = '" + main.g_org + "' and main_dist<>'D'and  warehouse='" + this.tb_warehouse.Text.Trim() + "'";
                    if (main.dbtype == "SQL")
                    {
                        dscustomer_area = main.dbservice.Sql_exec_selectsql(str_sql_area);
                    }
                    else
                    {
                        dscustomer_area = main.dbservice.oracle_exec_selectsql(str_sql_area);
                    }

                } 
                catch (System.Exception ee)
                {
                    MessageBox.Show(main.g_class.comm_getchangestring("Fail to save", main.g_info_dataset) + "!" + ee.ToString());
                    Cursor.Current = Cursors.Default;
                    return;
                }
            }

            f_setbutton("", true);
            ClearCntrValue(this.groupBox4, false, "1");

        }

        private void btn_cancel_area_Click(object sender, EventArgs e)
        {
            f_setbutton("detail", true);
            ClearCntrValue(this.groupBox4, false, "1");
        }

        private void cbx_area_flag_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void dvg_warehouse_SelectionChanged(object sender, EventArgs e)
        {
            if (dvg_warehouse.CurrentRow != null && b_temp==true)
            {
                
                dscustomer_area = new DataSet();
                DataRow[] tr = dscustomer.Tables[0].Select("warehouse='" + dvg_warehouse.Rows[dvg_warehouse.CurrentRow.Index].Cells["warehouse"].Value.ToString() + "'");
                ClearCntrValue(this.groupBox2, false, "0");
                if (tr.Length>0)
                show_warehouse(tr[0]);
                string str_sql_area = "select * from " + main.g_space + ".mst_area where inv_org = '" + main.g_org + "' and main_dist<>'D'and  warehouse='" + this.tb_warehouse.Text.Trim() + "' order by area_code";
                if (main.dbtype == "SQL")
                {
                    dscustomer_area = main.dbservice.Sql_exec_selectsql(str_sql_area);
                }
                else
                {
                    dscustomer_area = main.dbservice.oracle_exec_selectsql(str_sql_area);
                }
                dvg_area.DataSource = dscustomer_area.Tables[0];
                ClearCntrValue(this.groupBox4, false, "0");
                if (dscustomer_area.Tables[0].Rows.Count > 0)
                {
                    show_area(dscustomer_area.Tables[0].Rows[0]);
                }
            }
        }

        private void dvg_area_SelectionChanged(object sender, EventArgs e)
        {
            if (dvg_area.CurrentRow != null)
            {
                if (dscustomer_area.Tables[0].Rows.Count > 0)
                {
                    DataRow[] tr = dscustomer_area.Tables[0].Select("area_code='" + dvg_area.Rows[dvg_area.CurrentRow.Index].Cells[0].Value.ToString() + "'");
                    if (tr.Length > 0)
                        show_area(tr[0]);
                }
            }
        }

        private void btn_query_batch_Click(object sender, EventArgs e)
        {
            w_query_batch wsingle = new w_query_batch();
            wsingle.ShowDialog();
            this.tb_warehouse_ch.Text = w_query_batch.str_qcontent;
        }
    }
}